//
//  UIImage+Extensions.swift
//  WeiboSwift
//
//  Created by hfwd on 2017/6/5.
//  Copyright © 2017年 hfwd. All rights reserved.
//

import Foundation


extension UIImage {
    
    /// 拉伸指定的图片，并且返回新的图片
    ///
    /// - Parameters:
    /// - size: 拉伸后的大小
    /// - Returns: 拉伸后的图片
    func adjustImage(size: CGSize?, bgColor: UIColor = UIColor.white, lineColor: UIColor = UIColor.lightGray) -> UIImage? {
        
        let size = size ?? self.size
       
        let rect = CGRect(origin: CGPoint(), size: size)
        
        UIGraphicsBeginImageContext(size)
        
        bgColor.setFill()
        
        UIRectFill(rect)

        //剪切圆角
        let path = UIBezierPath(ovalIn: rect)
        
        path.addClip()
        
        //设置内切圆
        lineColor.setStroke()
        
        path.lineWidth = 2.0
        
        path.stroke()
        
        draw(in: rect)
        
        let result = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return result
    }
}
